using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SeataNet.Core.Configuration;
using SeataNet.Core.DependencyInjection;
using SeataNet.Core.Services;

namespace SeataNet.Samples
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            // 创建主机构建器
            var builder = Host.CreateDefaultBuilder(args)
                .ConfigureServices((context, services) =>
                {
                    // 配置Seata
                    services.AddSeata(configuration =>
                    {
                        configuration.ApplicationId = "seata-net-sample";
                        configuration.TransactionServiceGroup = "my_tx_group";
                        configuration.Transport.ServerAddress = "http://localhost:8091";
                        configuration.Transport.HeartbeatInterval = 30000;
                        configuration.Transport.Serialization = "json";
                        configuration.Transport.Compressor = "none";
                        
                        configuration.DatabaseProxy.DataSource = "server=localhost;database=seata_sample;user=root;password=123456;";
                        configuration.DatabaseProxy.UserName = "root";
                        configuration.DatabaseProxy.Password = "123456";
                        configuration.DatabaseProxy.MinPoolSize = 5;
                        configuration.DatabaseProxy.MaxPoolSize = 20;
                    });
                    
                    // 注册示例服务
                    services.AddScoped<SampleOrderService>();
                    services.AddScoped<PaymentService>();
                    services.AddScoped<InventoryService>();
                });

            // 构建主机
            var host = builder.Build();

            // 初始化Seata服务
            var seataService = host.Services.GetRequiredService<ISeataService>();
            await seataService.InitializeAsync();

            Console.WriteLine("SeataNet 示例程序已启动");
            Console.WriteLine("请选择要运行的示例:");
            Console.WriteLine("1. AT模式示例");
            Console.WriteLine("2. TCC模式示例");
            Console.WriteLine("3. Saga模式示例");
            Console.WriteLine("4. XA模式示例");
            Console.WriteLine("5. 退出");

            while (true)
            {
                Console.Write("请输入选项 (1-5): ");
                var option = Console.ReadLine();

                switch (option)
                {
                    case "1":
                        await RunAtModeSampleAsync(host.Services);
                        break;
                    case "2":
                        await RunTccModeSampleAsync(host.Services);
                        break;
                    case "3":
                        await RunSagaModeSampleAsync(host.Services);
                        break;
                    case "4":
                        await RunXaModeSampleAsync(host.Services);
                        break;
                    case "5":
                        await seataService.DestroyAsync();
                        Console.WriteLine("程序已退出");
                        return;
                    default:
                        Console.WriteLine("无效选项，请重新输入");
                        break;
                }
            }
        }

        private static async Task RunAtModeSampleAsync(IServiceProvider services)
        {
            Console.WriteLine("\n=== AT模式示例 ===");
            var orderService = services.GetRequiredService<SampleOrderService>();
            
            try
            {
                var orderId = await orderService.CreateOrderAsync("user123", "product001", 2);
                Console.WriteLine($"订单创建成功，订单ID: {orderId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"订单创建失败: {ex.Message}");
            }
            
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }

        private static async Task RunTccModeSampleAsync(IServiceProvider services)
        {
            Console.WriteLine("\n=== TCC模式示例 ===");
            var paymentService = services.GetRequiredService<PaymentService>();
            
            try
            {
                var paymentId = await paymentService.ProcessPaymentAsync("order123", "user123", 100.00m);
                Console.WriteLine($"支付处理成功，支付ID: {paymentId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"支付处理失败: {ex.Message}");
            }
            
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }

        private static async Task RunSagaModeSampleAsync(IServiceProvider services)
        {
            Console.WriteLine("\n=== Saga模式示例 ===");
            var orderService = services.GetRequiredService<SampleOrderService>();
            
            try
            {
                var orderId = await orderService.ProcessOrderWithSagaAsync("user123", "product001", 2);
                Console.WriteLine($"Saga订单处理成功，订单ID: {orderId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Saga订单处理失败: {ex.Message}");
            }
            
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }

        private static async Task RunXaModeSampleAsync(IServiceProvider services)
        {
            Console.WriteLine("\n=== XA模式示例 ===");
            var orderService = services.GetRequiredService<SampleOrderService>();
            
            try
            {
                var orderId = await orderService.CreateOrderWithXaAsync("user123", "product001", 2);
                Console.WriteLine($"XA订单创建成功，订单ID: {orderId}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"XA订单创建失败: {ex.Message}");
            }
            
            Console.WriteLine("按任意键继续...");
            Console.ReadKey();
        }
    }
}
